// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors;

import org.dengues.core.editor.AbstractGenericGEFEditor;
import org.dengues.core.editor.AbstractGenericMultiPageEditor;
import org.dengues.core.editor.utils.GEFEditorUtils;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.editors.DenguesJavaEditor;
import org.dengues.designer.ui.process.editors.GEFComponentsEditor;
import org.dengues.designer.ui.process.editors.ProcessEditorInput;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.IEditorPart;

/**
 * qzhang class global comment. Detailled comment <br/>
 * 
 */
public class ProcessMultiEditor extends AbstractGenericMultiPageEditor {

    public static final String ID = "org.dengues.designer.ui.editors.processMultiEditor"; //$NON-NLS-1$

    private final GEFComponentsEditor gefCompEditor = new GEFComponentsEditor();

    private final DenguesJavaEditor javaEditor = new DenguesJavaEditor();

    private boolean propertyIsDirty = false;

    /**
     * qzhang ProcessMultiEditor constructor comment.
     */
    public ProcessMultiEditor() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.MultiPageEditorPart#pageChange(int)
     */
    @Override
    protected void pageChange(int newPageIndex) {
        super.pageChange(newPageIndex);
        if (newPageIndex == 1) {
            try {
                javaEditor.doSetInput(getEditorInput());
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.MultiPageEditorPart#createPages()
     */
    @Override
    protected void createPages() {
        setPartName(((ProcessEditorInput) getEditorInput()).getName());
        createPage0();
        createPage1();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createPage1".
     */
    private void createPage1() {
        try {
            int index = addPage(javaEditor, getEditorInput());
            setPageText(index, Messages.getString("ProcessMultiEditor.Code.Label")); //$NON-NLS-1$
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createPage0() {
        try {
            int index = addPage(gefCompEditor, getEditorInput());
            setPageText(index, Messages.getString("ProcessMultiEditor.Component.Label")); //$NON-NLS-1$
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public void doSave(IProgressMonitor monitor) {
        if (!isDirty()) {
            return;
        }
        getEditor(0).doSave(monitor);

        propertyIsDirty = false;

        firePropertyChange(IEditorPart.PROP_DIRTY);
    }

    @Override
    public boolean isDirty() {
        return propertyIsDirty || super.isDirty();
    }

    public GEFComponentsEditor getGefCompEditor() {
        return this.gefCompEditor;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.MultiPageEditorPart#dispose()
     */
    @Override
    public void dispose() {
        GEFEditorUtils.removeDisposedEditor(getGefCompEditor());
        super.dispose();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.editor.AbstractGenericMultiPageEditor#getDesignerEditor()
     */
    @Override
    public AbstractGenericGEFEditor getDesignerEditor() {
        return gefCompEditor;
    }
}
